package com.library.bookmanagement.exception.handler;

import com.library.bookmanagement.exception.LoginFailException;
import com.library.bookmanagement.exception.PasswordException;
import com.library.bookmanagement.service.LoginService;
import com.library.bookmanagement.util.ResultUtil;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author ：ZXM
 * @date ：Created in 2020/5/6 17:03
 * @description：全局异常处理
 */
@ControllerAdvice
// 返回数据
@ResponseBody
public class GlobalExceptionHandler {

    // 指出异常执行的方法
    @ExceptionHandler(Exception.class)
    public ResultUtil error(Exception e){
        e.printStackTrace();
        return ResultUtil.error().message("执行了全局异常的处理...");
    }

    // 空指针异常处理方式
    @ExceptionHandler(NullPointerException.class)
    public ResultUtil error(NullPointerException e){
        e.printStackTrace();
        return ResultUtil.error().message("执行了NullPointerException空指针异常...");
    }

    //自定义异常
    @ExceptionHandler(PasswordException.class)
    public ResultUtil error(PasswordException e){
        e.printStackTrace();
        return ResultUtil.error().code(e.getCode()).message(e.getMessage());
    }
    @ExceptionHandler(LoginFailException.class)
    public ResultUtil error(LoginFailException e){
        e.printStackTrace();
        return ResultUtil.error().message("登录失败错误....!");
    }

}
